<?php
/**
 * @category 	DateUtil.php
 * @author 		Sung Hoon, Cho (akdddnet@gmail.com)
 * @since		2011. 4. 11.
 * @license		BSD License
 * @version		0.1
 */

class DateUtil {
	
	static function getDateDiff($t1, $t2) {
		$datetime1 = new DateTime($t1);
		$datetime2 = new DateTime($t2);
		$interval = $datetime1->diff($datetime2);
		
		return $interval;
	}
	
	static function getDateDiffFormat($t1, $t2, $format = "%R%a") {
		$interval = self::getDateDiff($t1, $t2);
		return $interval->format($format);
	}
	
	static function getDateDiff2($t1, $t2) { 
	    if(!$t2) return; 
	    $trans = array('y' => '년', 'm' => '개월', 'd' => '일', 'h' => '시간', 'i' => '분', 's' => '초'); 
	    $ago = array(' 후', ' 전'); 
	    $t1 = new DateTime($t1); 
	    $t2 = new DateTime($t2); 
	    $t1 = $t1->diff($t2); 
	    foreach($t1 as $key => $val) 
	        if($val) return $diff = $val . $trans[$key] . $ago[$t1->invert]; 
	
	    return 'error'; 
	} 
	
	static function getTimeToDate($datetime, $format="Y-m-d") {
		return date($format, strtotime($datetime));
	}
}